﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using LoadingB.Config;
using System.Drawing;
using LoadingB.Windows;

namespace LoadingB
{
    public partial class Form_Main : Form
    {
        // 创建 浏览器 控件
        private WebView2 Browser;
        // 创建 托盘图标 控件
        private ContextMenuStrip contextMenu;


        public Form_Main()
        {
            // 初始化窗体
            InitializeComponent();

            // 初始化浏览器
            InitializeWebView2();

            // 根据托盘选项变量决定是否初始化托盘图标
            if (Variable.Tray == 1)
            {
                InitializeTrayIcon();
            }

            // 设置控件加载过程
            this.SetControl();
        }

        // 初始化浏览器
        private void InitializeWebView2()
        {
            this.Browser = new WebView2();
            this.Browser.Dock = DockStyle.Fill;
            this.Controls.Add(this.Browser);
        }


        // 初始化托盘图标
        private void InitializeTrayIcon()
        {
            // 创建上下文菜单
            contextMenu = new ContextMenuStrip();
            var exitMenuItem = new ToolStripMenuItem("退出", null, ExitMenuItem_Click);
            var setupMenuItem = new ToolStripMenuItem("设置", null, SetupMenuItem_Click);
            var aboutMenuItem = new ToolStripMenuItem("关于", null, AboutMenuItem_Click);
            contextMenu.Items.Add(aboutMenuItem);
            contextMenu.Items.Add(setupMenuItem);
            contextMenu.Items.Add(exitMenuItem);

            // 创建托盘图标
            TrayIcon.ContextMenuStrip = contextMenu;
            // 设置托盘图标属性
            TrayIcon.Visible = true;
            // 处理双击事件
            TrayIcon.DoubleClick += NotifyIcon_DoubleClick;
        }

        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            // 显示窗体
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void SetupMenuItem_Click(object sender, EventArgs e)
        {
            // 显示设置对话框
            Form_Setup formSetup = new Form_Setup();
            formSetup.ShowDialog();
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            // 退出应用程序
            Application.Exit();
        }

        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            // 显示关于对话框
            Form_About aboutForm = new Form_About();
            aboutForm.ShowDialog();
        }


        // 设置控件加载过程
        public async void SetControl()
        {
            //// 创建 ConfigManager 对象
            ConfigManager configManager = new ConfigManager();

            if (Screen.AllScreens.Length >= 1)
            {
                // 获取第二个显示器的屏幕对象
                Screen secondScreen = Screen.AllScreens[Variable.Screen];
                // 将窗体的启动位置设置为手动
                this.StartPosition = FormStartPosition.Manual;
                // 将窗体的位置设置为第二个显示器的左上角
                this.Location = secondScreen.Bounds.Location;
                // 将窗体的大小设置为第二个显示器的大小
                this.Size = secondScreen.Bounds.Size;
                // 设置窗体为无边框
                this.FormBorderStyle = FormBorderStyle.None;
                // 设置窗体为顶层
                this.TopMost = true;
                // 设置窗体为最大化
                this.WindowState = FormWindowState.Maximized;

                // 创建 WebView2 环境并指定用户数据目录
                string userDataFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebView2");
                var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder, new CoreWebView2EnvironmentOptions("--disable-application-cache"));
                await this.Browser.EnsureCoreWebView2Async(environment);

                // 订阅 DOMContentLoaded 事件
                this.Browser.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
                // 设置浏览器显示内容
                this.Browser.Source = new Uri(Variable.Url);   // new Uri("http://127.0.0.1/");
            }
        }

        // DOMContentLoaded 事件处理程序
        private void CoreWebView2_DOMContentLoaded(object sender, CoreWebView2DOMContentLoadedEventArgs e)
        {
            // 获取屏幕大小
            var screenSize = Screen.AllScreens[Variable.Screen].Bounds.Size;

            // 执行 JavaScript 获取网页大小并设置缩放比例
            this.Browser.CoreWebView2.ExecuteScriptAsync(@"
                (function() {
                    var body = document.body;
                    var html = document.documentElement;
                    var pageWidth = Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth);
                    var pageHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
                    var scaleX = " + screenSize.Width + @" / pageWidth;
                    var scaleY = " + screenSize.Height + @" / pageHeight;
                    var scale = Math.min(scaleX, scaleY);
                    document.body.style.zoom = scale;
                })();
            ");
        }

        private void Browser_Click(object sender, EventArgs e)
        {
            // 退出应用程序
            Application.Exit();
        }
    }
}

